package controllers

import (
	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
	"net/http"
	"shimmer/neusoft_club_backend/controllers/utils"
	"shimmer/neusoft_club_backend/dao/domain"
	"shimmer/neusoft_club_backend/services"
)

func handlePing(c *gin.Context) {
	c.JSON(200, gin.H{
		"message":    "pong",
		"user_service-agent": c.GetHeader("user_service-agent"),
	})
}
func getAllUsers(c *gin.Context) {
	pageQuery := utils.PaginationQuery{}
	err := c.ShouldBind(&pageQuery)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"error": err.Error(),
		})
		return
	}
	users := make([]domain.User, 0)
	db := services.DBEngine.Joins("Avatar")
	paginate, err := utils.QueryAndPaginate(&pageQuery, db, &users)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"error": err.Error(),
		})
		return
	}
	c.JSON(200, paginate)
}
func createUser(c *gin.Context) {
	newUser := domain.User{Nickname: "test"}
	err := services.DBEngine.Create(&newUser).Error
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"success": false,
		})
		return
	}
	c.JSON(200, gin.H{
		"success": true,
	})
}
func sessionTest(c *gin.Context) {
	store := sessions.Default(c)
	count := store.Get("count")
	if count == nil {
		count = 0
	}
	countNumber := count.(int) + 1
	store.Set("count", countNumber)
	err := store.Save()
	if err != nil {
		return
	}
	c.JSON(200, gin.H{
		"count": countNumber,
	})
}
func ConfigTestController(groupConfig *gin.RouterGroup) {
	groupConfig.GET("/ping", handlePing)
	groupConfig.GET("/allUsers", getAllUsers)
	groupConfig.GET("/createUser", createUser)
	groupConfig.GET("/session", sessionTest)
}
